/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.modulo.carga;

import br.com.bld.model.cliente.ClienteBe;
import br.com.bld.model.cliente.ClienteEntity;
import br.com.bld.model.cliente.ClientePK;
import br.com.bld.model.endereco.EnderecoBe;
import br.com.bld.model.endereco.EnderecoEntity;
import br.com.bld.model.municipio.MunicipioBe;
import br.com.bld.model.telefone.TelefoneBe;
import br.com.bld.model.telefone.TelefoneEntity;
import br.com.bld.util.conexao.ConexaoPGSQL;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Pedro
 */
public class CargaClientes {

    public static void main(String[] args) throws IOException, SQLException {

        System.out.println(new Date());

        ClienteBe c = null;
        EnderecoBe e = null;
        TelefoneBe t = null;
        Connection conn = null;

        try {
            conn = ConexaoPGSQL.getConection();

            c = new ClienteBe(conn);
            e = new EnderecoBe(conn);
            t = new TelefoneBe(conn);
        } catch (SQLException ex) {
            ex.printStackTrace();

        }
        File file = new File("Anastacio.txt");

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));

        String linha = br.readLine();

        while (linha != null) {

            String[] tokens = linha.split("\\;", 7);

            String nome = tokens[0].trim();
            String[] telefone = tokens[1].trim().split("\\,");
            String fax = tokens[2].trim();
            String logradouro = tokens[3].trim();
            String numero = tokens[4].trim();
            String bairro = tokens[5].trim();
            String complemento = tokens[6].trim();

            ClienteEntity clienteEntity = c.selecionarPorNomeFantasia(nome);

            if (clienteEntity == null) {
                clienteEntity = new ClienteEntity();

                clienteEntity.setCategoriaId(1L);
                clienteEntity.setCnpj(null);
                clienteEntity.setContatoId(null);
                clienteEntity.setInscricaoEstadual(null);
                clienteEntity.setNomeFantasia(nome);
                clienteEntity.setPlanoId(null);
                clienteEntity.setRazaoSocial(null);

                ClientePK clientePK = c.inserir(clienteEntity);

                EnderecoEntity enderecoEntity = new EnderecoEntity();

                if (!logradouro.equals("")) {
                    enderecoEntity.setBairro(bairro);
                    enderecoEntity.setCep(79210000);
                    enderecoEntity.setCodgIbge(5000708);
                    enderecoEntity.setComplemento(complemento);
                    enderecoEntity.setLogradouro(logradouro);
                    enderecoEntity.setNumero(numero);

                    e.inserir(enderecoEntity, clientePK.getId());
                }

                for (String telefone1 : telefone) {

                    String str = "67" + telefone1.replace("-", "");

                    if (t.selecionarPorNumrFone(Long.parseLong(str)).size() == 0) {
                        TelefoneEntity telefoneEntity = new TelefoneEntity();
                        Long numrFax = Long.parseLong("67" + fax.replace("-", "").trim());
                        telefoneEntity.setNumrFone(Long.parseLong(str.trim()));
                        telefoneEntity.setNumrFax(numrFax);
                        t.inserir(telefoneEntity, clientePK.getId());
                        
                        conn.commit();
                    }
                }

                
            } else {
                System.out.print(linha);
            }

            linha = br.readLine();
        }

        System.out.println(new Date());
    }

    private static String getNome(String linha) {

        String resposta = "";

        for (int i = 0; i < linha.length(); i++) {
            if (i + 1 < linha.length() && linha.charAt(i) == '.' && linha.charAt(i + 1) == '.') {
                break;
            }

            resposta += linha.charAt(i);
        }

        return resposta;

    }
}
